package org.goodsl.deploymap.loader

import groovy.io.FileType
import org.goodsl.deploymap.ScriptLoader

/**
 * Created with IntelliJ IDEA.
 * User: Simon Xiao
 * Date: 12-12-19
 * Time: 下午3:20
 * Copyright goodsl.org 2012~2020
 */
class DiskFolderLoader implements ScriptLoader{

    File folder

    HashMap<String,File> files = new HashMap<String,File>()

    public  DiskFolderLoader(String floderPath){
        File folder = new File(floderPath)
        if (!folder.isDirectory()){
            throw new Exception("DiskFolderLoader need Folder path ")
        }

        folder.eachFileMatch (FileType.FILES,~/.*\.gdsl/){File file ->

            //file.name

            files.put(file.name,file)
        }

    }

    String load(String scriptName) {
        if (!files.containsKey(scriptName))
            return  null

        File file = files.get(scriptName)
        file.toString()

        FileInputStream fis = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(fis, "UTF-8");


        //FileReader reader = new FileReader(file)
        //String ec = reader.getEncoding()
        return reader.text


    }

    String[] scripts() {
        return files.keySet().toArray()
    }
}
